﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

using VNet.VisiSuite.Common.Logger;
using VNet.VisiSuite.Common.Production;
using VNet.VisiSuite.Common.Helpers;
using Infragistics.Controls.Charts;
using System.ComponentModel;
using VNet.VisiSuite.Service.Contact.Message.ProductionData;
using Infragistics;

namespace VNet.VisiSuite.Controls
{

     /// <summary>
    /// Interaction logic for ProductionControl.xaml
    /// </summary>
    public partial class UtilizationGraphControl : UserControl, INotifyPropertyChanged
    {

        #region Member variables
        
        List<UtilizationGraphDataValue> _series_data;
        double _max_y_value = 100.0;
        
        #endregion

        #region Dependencies

        public static readonly DependencyProperty GraphDataProperty = DependencyProperty.Register("GraphData", typeof(List<GraphDataItem>), typeof(UtilizationGraphControl), new FrameworkPropertyMetadata(null, GraphDataChangedCallback));

        public List<GraphDataItem> GraphData
        {
            get { return null; }
            set { SetValue(GraphDataProperty, value); }
        }

        private static void GraphDataChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ctrl = d as UtilizationGraphControl;
            var data = e.NewValue as List<GraphDataItem>;
            ctrl.SetData(data);
        }

        #endregion

        #region Properties

        public string GraphTitle
        {
            get { return "PLANT UTILIZATION BY HOUR"; }
        }

        public double MaxValue
        {
            get { return _max_y_value; }
        }

        public List<UtilizationGraphDataValue> SeriesData
        {
            get { return _series_data; }
        }

        #endregion

        public UtilizationGraphControl()
        {

            InitializeComponent();

        }


        private string formatHour(int hr)
        {
            string val;

            if (hr == 0)
                val = "12 AM";
            else if (hr < 12)
                val = hr.ToString() + " AM";
            else if (hr == 12)
                val = "12 PM";
            else
                val = (hr-12).ToString() + " PM";

            return val;
        }

        public void SetData(List<GraphDataItem> raw_data)
        {
            _max_y_value = 100.0;
            _series_data = new List<UtilizationGraphDataValue>();
            foreach (GraphDataItem gdi in raw_data)
            {
                _series_data.Add(new UtilizationGraphDataValue {
                    Key = formatHour((int)gdi.XVal),
                    Percent = gdi.YVal
                });

                if (gdi.YVal > 95.0)
                    _max_y_value = 105.0;
            }

            OnPropertyChanged("MaxValue");
            OnPropertyChanged("SeriesData");
            
        }
       
        #region Implement INotifiedPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

    }

    public class UtilizationGraphDataValue
    {
        private string _key;
        private double? _percent;
        private string _percent_marker;

        public string Key
        {
            get { return _key; }
            set { _key = value; }
        }

        public string PercentMarker { get { return _percent_marker; } }    
        
        public double? Percent { 
            get { return _percent; }
            set
            {
                _percent = value > 0.0 ? value : (value > 100.0 ? 100.0 : (double?)null);
                _percent_marker = _percent.HasValue ? Convert.ToInt32(_percent).ToString() + "%" : null;
            }
        }

    }

}
